import { GET } from './utils/methods';

App({
	onLaunch: function (options) {
		this.start(options);
	},
	start: async function (options) {
		// 1.获取 deptId
		this.globalData.deptId = options.query.deptId ? options.query.deptId : null;
		// 2.登录 发起 miniLogin 请求
		let r = await this.login();
		// 3.如果登录失败则再次提交
		if (!r) {
			this.start(options);
		}
	},
	next: function () {
		// 4.获取医院信息
		const hospital = this.getHospitalInfo();
		// 5.获取用户列表
		const userList = this.getUserList();
		Promise
			.allSettled([ hospital, userList ])
			.then(res => {
				this.globalData.hospitalInfo = res[0].value.data
				this.globalData.userList = res[1].value.data;
				this.globalData.is_login = true;
				this.globalData.currentPatient = res[1].value.data.filter(item => item.id == this.globalData.userId)[0];
			});
	},
	login: function () {
		console.log(5555)
		return new Promise(async resolve => {
			const code = await this.getWxCode();
			const params = {
                grant_type: 'authorization_code',
                alias: this.globalData.config.alias,
                code: code,
                deptId: this.globalData.deptId
			};
			GET('/wechat/oauth/miniLogin', params).then(res => {
				if (res.code == 0) {
					// 赋值
					app.globalData.deptId = res.data.ptDefaultDeptId;
					app.globalData.queueId = res.data.ptDefaultQueueId;
					app.globalData.userId = res.data.ptDefaultUserId;
					app.globalData.userMobile = res.data.mobile;
					app.globalData.account = res.data.ptAccountId;
					app.globalData.openid = res.data.openid;
					let header = {};
					header.Cookie = 'JSESSIONID=' + res.data.sessionId;
					wx.setStorageSync("cookie", header);
					if (res.data.miniBackEndUrl !== app.globalData.config.server) {
						app.globalData.config.server = res.data.miniBackEndUrl;
						app.globalData.config.oriServer = res.data.miniBackEndUrl,
						app.globalData.config.educationServer = res.data.miniBackEndUrl
						return resolve(false);
					}
					return resolve(true);
				} else {
					return resolve(false);
				}
			}).catch(() => { resolve(false); });
		});
	},
	getWxCode: function () {
		return new Promise(resolve => {
            wx.login({
                success: function (res) {
                    resolve(res.code);
                }
            });
        });
	},
	getHospitalInfo: function () {
		return GET('/xcx/queryHospDept', { deptId: app.globalData.deptId });
	},
	getUserList: function () {
		return GET('/xcx/pt/getUsersInfo');
  },
  
 
	getUrl: function (url) {
		if (!url.startsWith('/')) url = "/" + url
		return this.globalData.config.server + url;
	},
	getEducationUrl: function(url) {
		if (!url.startsWith('/')) url = "/" + url
		return this.globalData.config.educationServer + url;
	},
	//书写全局变量
	globalData: {
		is_login: false,
		code: null,
		hospitalInfo: null,
		currentPatient: null,
		userInfo: null,
		userId:null,
		queueId: null,
		userMobile: null,
		deptId:null,
		openid: null,
		codeDeptId:null,
		forwarded:false,
		statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
		header: {
		'Cookie': ''
		},
		userList:null,
		safe_bottom: 0,
		/**
		 * 正式环境 接口地址
		 * 新生儿
		 */
		// config: {
		// alias: "prod",
		// server: "https://xcx.bb-care.com/bbCare",
		// oriServer: "https://xcx.bb-care.com/bbCare",
		// educationServer: "https://xcx.bb-care.com/bbCare"
		// },
		// config: {
		//   alias: "blood_dev",
		//   server: "https://xcx.bb-care.com/blood_bbCare",
		//   oriServer: "https://xcx.bb-care.com/blood_bbCare",
		//   educationServer: "https://xcx.bb-care.com/blood_bbCare"
		// },
		// config: {
		//   alias: "FuEr",
		//   server: "https://xcx.bb-care.com/bbCare",
		//   oriServer: "https://xcx.bb-care.com/bbCare",
		//   educationServer: "https://xcx.bb-care.com/bbCare"
		// },
		// config: {
		//   alias: "earlyChildhood",
		//   server: "https://xcx.bb-care.com/earlyChildhood",
		//   oriServer: "https://xcx.bb-care.com/earlyChildhood",
		//   educationServer: "https://xcx.bb-care.com/earlyChildhood"
		// },
		
		/**
		 * 本地环境 接口地址 
		 */
		config: {
		  alias: "dev",
		  server: 'http://192.168.1.199:9020/platform',
		  oriServer: 'http://192.168.1.199:9020/platform',
		  educationServer: 'http://192.168.1.199:9020/platform',
		  archiveServer: 'http://192.168.1.199:9020/platform'
		}
		
	}
})

const app = getApp();
export default app;